package com.taomu.app.zongchouhuli.http.api;

import android.util.Log;

import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.entity.BaseResponse;
import com.taomu.app.zongchouhuli.entity.BaseResponse2;

import io.reactivex.Observable;
import io.reactivex.functions.Function;



public class BaseResponseCode<T> implements Function<BaseResponse<T>, Observable<BaseChangeResponse<T>>> {

    private final String code = "200";

    @Override
    public Observable<BaseChangeResponse<T>> apply(BaseResponse<T> baseDateBaseResponse) throws Exception {
        //遇到非200错误统一处理,将BaseResponse继续向下转换成对应实体
        Log.d("dwqdwqdqdwqd","code:"+baseDateBaseResponse.getRet());
        if (code.equals(baseDateBaseResponse.getRet())) {
            Log.d("dwqdwqdqdwqd","success:"+baseDateBaseResponse.getRet());
            BaseChangeResponse<T> data = new BaseChangeResponse<>();
            data.setData(baseDateBaseResponse.getData());
            data.setCount(baseDateBaseResponse.getCount());
            return Observable.just(data);
        } else {
            Log.d("dwqdwqdqdwqd","error:msg:"+baseDateBaseResponse.getMsg());
            return Observable.error(new Throwable(baseDateBaseResponse.getMsg()));
        }
    }
}
